﻿using System;
using System.IO;
using System.Linq;

namespace TddBarForPhone
{
    class Program
    {
        private const int LinesToSkip = 3;

        static void Main(string[] args)
        {
            if(args.Length < 2)
            {
                Help();
                return;
            }

            var nunitConsolePath = args[0];
            if(!File.Exists(nunitConsolePath))
            {
                Console.WriteLine("File not found : " + nunitConsolePath);
                return;
            }
            
            var dllPath = args[1];
            if(!File.Exists(dllPath))
            {
                Console.WriteLine("File not found : " + dllPath);
                return;
            }
            
            var buildWatcher = new BuildWatcher();
            buildWatcher.Launch(dllPath);
            var nunit = new NunitTask(nunitConsolePath, dllPath, "/nologo /nodots /framework=4.0.30319");
            InitTestResultSmoothly(nunit);
            buildWatcher.KeepUpdatingForEver((se, evt) => UpdateTestResultScreenByScreen(nunit));
        }

        private static void Help()
        {
            Console.Write(@"This program launches NUnit against a windows phone dll anytime it's built, with ");
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write("c");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("o");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("l");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("o");
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("r");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("s");
            Console.ResetColor();
            Console.WriteLine(".");
            Console.WriteLine("usage : TddBarForPhone.exe {nunit-console full path} {dll to test}");
            Console.WriteLine();
            Console.WriteLine(@"exemple of result :");
            ConsoleTddPainter.WriteLine(@"       [LOREM IPSUM] Lorem ipsum dolor sit amet");
            ConsoleTddPainter.WriteLine(@"       Tests run: 20, Errors: 0, Failures: 0, Inconclusive: 0, Time: 0,046875 seconds");
            ConsoleTddPainter.WriteLine(@"       Tests run: 20, Errors: 1, Failures: 0, Inconclusive: 0, Time: 0,046875 seconds");
        }

        private static void InitTestResultSmoothly(NunitTask nunit)
        {
            Console.WriteLine(DateTime.Now);
            var i = 1;
            nunit.RunTests(line =>
                               {
                                   if (i > LinesToSkip)
                                       ConsoleTddPainter.WriteLine(line);
                                   i++;
                               });
        }

        private static void UpdateTestResultScreenByScreen(NunitTask nunit)
        {
            Console.WriteLine("Tests in progress...");
            var now = DateTime.Now;

            var lines = nunit.RunTests();

            Console.Clear();
            Console.WriteLine(now);
            foreach (var line in lines.Skip(LinesToSkip))
            {
                ConsoleTddPainter.WriteLine(line);
            }
        }
    }
}
